<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<fmt:setBundle basename="infomessages" var="info"/>

<%
	request.setAttribute("path", request.getContextPath());
%>
<script type="text/javascript" charset="UTF-8">

function selectFile(formId,inputFileId ){
	$("#"+formId+" [id='"+inputFileId+"']").click();
	return false;
}
function checkFile(obj,fileType,fileCount,formId){
	var $file = $(obj);
	$file.siblings("input[name='file_path']").val($file.val());
    var fileName = $file.val();
    if(!fileName){
 	   alert("请选择文件");
 	   return false;
    }
    
 	if(fileCount){
    	
    	var realCount =  $("#"+formId+" span[id*='uploadedFileName']").size();
    	if(realCount >= fileCount){
    		alert("此处最能能上传"+fileCount+"个文件！请先删除一部分文件再上传.")
    		return false;
    	}
    }
    if(fileType == 'text'){
    	
    	if(!common.checkFileText(fileName)){return false;}
    	
    }else if(fileType == 'image'){
    	
    	if(!common.checkFileImage(fileName)){return false;}
 		
    }
    var auto = $("#autoUpload").val();
    if(auto){
    	// $("#uploadBtn").trigger("click");
    	uploadFile(formId,$file.attr("id"),fileType,fileCount);
    }
 
    return true;
};
function uploadFile(formId,inputFileId,fileType,fileCount){
	var obj = $("#"+formId+" [id='"+inputFileId+"']");
	// 非自动上传的时候才重新检查
	var auto = $("#autoUpload").val();
	if(!auto){
		if(!checkFile(obj,fileType,fileCount,formId)){
			return false;
		}
	}
	var uri = $("#"+formId).attr("action");
	$("body").everyTime('200ms','updateProgress',function(){
		 updateProgress(formId,inputFileId);
	});
	$.ajaxFileUpload ({
	    url:uri, 
	    secureuri:false, 
	    fileElementId:inputFileId,
	    dataType:'json', 
	    success: function (result) {
	    	 if(result.type == "1"){
			    common.alertModeDialog("error","错误","上传文件失败","common.closeDialog('error')"); 
	   		 }else if(result.type == "3"){
	   			 
	   			 var id = "<input type='hidden' name='uploadFileId' id='uploadFileId@0' value='@0'/>";
	   			 id = common.replaceAll(id,"@0",result["content"]["id"]);
	   			 $("#uploadedFileIds").append(id );
	   			 var name = "<span id='uploadedFileName[0]' >[1]&nbsp;&nbsp;</span>";
	   			 name = name.replace("[0]",result["content"]["id"]);
	   			 name = name.replace("[1]",result["content"]["name"]);
	   			 //var name = $("#uploadFileName").clone().attr("id",result["content"]["id"]).text(result["content"]["name"]);
	   			 var img = '<img src="${path}/portal/css/images/del.jpg" id="img@0" onclick="removeAtt(@0)" style="vertical-align:middle;cursor:pointer"></img>';
	   			 img = common.replaceAll(img,"@0",result["content"]["id"]);
	   			 $("#"+formId+" [id='uploadedFileNames']").append(name).append(img);
	   			try{
					 if(typeof(eval("uploadFileCallback"))=="function") {
						 uploadFileCallback(result["content"]["id"],result["content"]["name"],result);
					 }
				}catch(e){
					
				}
	   		 }else{
	   			 alert("unknown error"); 
	   		 }
	    }
	  });
}

function removeAtt(id ){
	 $("#uploadedFileName"+id).remove();
	 $("#img"+id).remove();
	 $("#uploadFileId"+id).remove();
	 try{
		 if(typeof(eval("removeFileCallback"))=="function") {
			 removeFileCallback(id);
		 }
	}catch(e){
		
	}
}
function updateProgress(formId, inputFileId){
	 $.post("${root}/view/upload/progress?fileId="+inputFileId,function(progress){
		 $("#"+formId+" [id='percent']").text(progress);
		 if(progress >= 100){
			 $("body").stopTime ('updateProgress');
		 }
	 });
	
}

</script>
<input type="hidden" id="autoUpload" value="${param.autoUpload }" />
<form  id="${param.formId==null?'uploadForm':param.formId}" action="${param.uri }?fileId=${param.fileId}" method="post">
	<%-- <input type="file" onchange="checkFile(this,'${param.fileType}','${param.fileCount }','${param.formId==null?'uploadForm':param.formId}')" name="${param.fileId }"  id="${param.fileId }" style="display:none;" > --%>
	<table>
		<tr>
			<td>
				<span class="file_container" style="text-align: center;">
					<input name="file_path" size="10" type="text"  class="txt">
					<input type="file" class="file" onchange="checkFile(this,'${param.fileType}','${param.fileCount }','${param.formId==null?'uploadForm':param.formId}')" name="${param.fileId }"  id="${param.fileId }" >
					<button type="button" name="file_select" class="btn">
						浏览
					</button>
					<span></span> 
				</span>
				<c:if test="${param.autoUpload }">
					<span style="display: none;">
						&nbsp;&nbsp;<button id="uploadBtn" type="button" onclick="uploadFile('${param.formId==null?'uploadForm':param.formId}','${param.fileId }','${param.fileType}','${param.fileCount }')" class="btn">上传</button>
					</span>
				</c:if>
				<c:if test="${not param.autoUpload }">
					&nbsp;&nbsp;<button id="uploadBtn" type="button" onclick="uploadFile('${param.formId==null?'uploadForm':param.formId}','${param.fileId }','${param.fileType}','${param.fileCount }')" class="btn">上传</button>
				</c:if>
				&nbsp;&nbsp;进度:<span id="percent">0</span>%
			</td>
		</tr>
		<tr>
			<td style="text-align: left;" id="uploadedFileNames">
				<c:forEach items="${attachments}" var="item">
					<span id='uploadedFileName${item.id }' ><c:out value="${item.name }"/></span>
					&nbsp;<img src="${path}/portal/css/images/del.jpg" id="img${item.id}" onclick="removeAtt(${item.id})" style="vertical-align:middle;cursor:pointer"></img>
				</c:forEach>
			</td>
		</tr>
	</table>
</form>

